package com.linkoog.devtools.hide_project_path;

import com.intellij.ide.projectView.TreeStructureProvider;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 隐藏项目根目录上的路径
 */
public class CustomTreeStructureProvider implements TreeStructureProvider {

    @Override
    public @NotNull Collection<AbstractTreeNode<?>> modify(@NotNull AbstractTreeNode<?> parent, @NotNull Collection<AbstractTreeNode<?>> children, ViewSettings settings) {
        ArrayList<AbstractTreeNode<?>> nodes = new ArrayList<>();
        for (AbstractTreeNode<?> child : children) {
            Project project = child.getProject();
            if (project == null) {
                nodes.add(child);
                continue;
            }

            final VirtualFile baseDir = project.getBaseDir();
            if (child.getValue() instanceof PsiDirectory) {
                PsiDirectory directory = (PsiDirectory) child.getValue();
                if (directory.getVirtualFile().equals(baseDir)){
                    nodes.add(new PsiDirectoryNodeCustom(project, directory, settings));
                    continue;
                }
            }
            nodes.add(child);
        }
        return nodes;
    }


}
